package com.jk.dailydemo.progressBar.customProgress;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.Point;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jk.dailydemo.R;
import com.jk.dailydemo.utils.JKUtil;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by jk on 2017/6/8.
 */

public class CustomProgress extends RelativeLayout {
    @BindView(R.id.rl_show_progress)
    RelativeLayout mRlShowProgress;
    @BindView(R.id.pb_progress)
    ProgressBar mProgressBar;
    @BindView(R.id.tv_show_progress)
    TextView mTvCurrentProgress;
    @BindView(R.id.iv_progress_head)
    ImageView mIvProgressHead;



    private int windowWidth;
    private int windowHeight;
    private LinearLayout.LayoutParams param;
    private LinearLayout.LayoutParams pBParams;



    private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 0:
                    int current = (int)msg.obj;
                    mProgressBar.setProgress(current);
                    int max = mProgressBar.getMax();
                    int width = mProgressBar.getWidth();

                    if (pBParams == null){
                        pBParams = (LinearLayout.LayoutParams) mProgressBar.getLayoutParams();
                    }
                    int pBmarginStart = pBParams.getMarginStart();


                    int measureStart = width* current/max;
                    mTvCurrentProgress.setText(current+"%");
                    if (param == null){
                        param = (LinearLayout.LayoutParams) mRlShowProgress.getLayoutParams();
                    }

                    param.setMarginStart((int) (pBmarginStart+measureStart - (mRlShowProgressWidth+0.5)/2));

                    mRlShowProgress.setLayoutParams(param);

                    break;


            }
        }
    };
    private int progressTest = 0;
    private int mRlShowProgressWidth;

    @Override
    public void onWindowFocusChanged(boolean hasWindowFocus) {
        super.onWindowFocusChanged(hasWindowFocus);
        param = (LinearLayout.LayoutParams) mRlShowProgress.getLayoutParams();
        pBParams = (LinearLayout.LayoutParams) mProgressBar.getLayoutParams();

        mRlShowProgressWidth = mRlShowProgress.getWidth();
        Point screenMetrics = JKUtil.getScreenMetrics(getContext());
        windowWidth = screenMetrics.x;
        windowHeight = screenMetrics.y;
        setup();
    }

    public CustomProgress(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);


//        mHandler.postDelayed(new Runnable() {
//            @Override
//            public void run() {
//                Message message = new Message();
//                message.what = 0;
//
//                progressTest += 1;
//                if (progressTest == 100){
//                    progressTest = 0;
//                }
//
//                message.obj = progressTest;
//                mHandler.sendMessage(message);
//
//                mHandler.postDelayed(this, 300);
//            }
//        }, 1000);
    }




    public CustomProgress(Context context) {
        this(context, null);
    }

    public CustomProgress(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        View inflate = LayoutInflater.from(context).inflate(R.layout.progress_custom, this, true);
        ButterKnife.bind(inflate, this);

        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomProgress, defStyle, 0);
         progress = a.getInteger(R.styleable.CustomProgress_progress, 0);
         headTextColor = a.getColor(R.styleable.CustomProgress_progress_text_color, Color.BLACK);
         headTextProgress = a.getString(R.styleable.CustomProgress_progress_text);
         progressColor = a.getResourceId(R.styleable.CustomProgress_progress_color,R.drawable.progress_bg_pink);
         progressHeadImage = a.getResourceId(R.styleable.CustomProgress_progress_head_image, R.mipmap.sign_data_2);
         a.recycle();





    }

    /*
     private int progress;
    private int headTextColor;
    private String headTextProgress;
    private int progressColor;
    private int progressHeadImage;
     */
    private void setup() {
        mProgressBar.setProgress(progress);
//        mProgressBar.progressDrawable
        mProgressBar.setProgressDrawable(getResources().getDrawable(progressColor));
        mTvCurrentProgress.setText(headTextProgress);
        mTvCurrentProgress.setTextColor(headTextColor);
        mIvProgressHead.setImageResource(progressHeadImage);

        setProgressMessage(progress);

    }
    private void setProgressMessage(int progress){

        Message message = new Message();
        message.what = 0;

        message.obj = progress;
        mHandler.sendMessage(message);
    }




    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    private int progress;
    private int headTextColor;
    private String headTextProgress;
    private int progressColor;
    private int progressHeadImage;

    public int getProgress() {
        return progress;
    }

    public void setProgress(int progress) {
        this.progress = progress;

        setup();
    }

    public int getHeadTextColor() {
        return headTextColor;
    }

    public void setHeadTextColor(int headTextColor) {
        this.headTextColor = headTextColor;
        setup();
    }

    public String getHeadTextProgress() {
        return headTextProgress;
    }

    public void setHeadTextProgress(String headTextProgress) {
        this.headTextProgress = headTextProgress;
        setup();
    }

    public int getProgressColor() {
        return progressColor;
    }

    public void setProgressColor(int progressColor) {
        this.progressColor = progressColor;
        setup();
    }

    public int getProgressHeadImage() {
        return progressHeadImage;
    }

    public void setProgressHeadImage(int progressHeadImage) {
        this.progressHeadImage = progressHeadImage;
        setup();
    }
}
